package ibmetp.mis.file;

import ibmetp.mis.common.Constants;
import ibmetp.mis.fileupload.MonitoredDiskFileItemFactory;
import ibmetp.mis.fileupload.UploadListener;
import ibmetp.mis.service.UserService;
import ibmetp.mis.serviceImpl.UserServiceImpl;
import ibmetp.mis.vo.User;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
 * 处理上传文件
 * @author LAN DABIN
 * @created 2011-10-1
 */
public class FileDealServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
  
    public FileDealServlet() {
        super();
       
    }

	
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		final long MAX_SIZE =500 * 1024 * 1024;// 设置 
		// 允许上传的文件格式的列表  
		  //final String[] allowedExt = new String[] { "jpg", "jpeg", "gif","png" }; 
		    UploadListener listener = new UploadListener(request, 30);
		    
		   // String loadpath=getServletContext().getRealPath("/")+"dir";
		   String loadpath="F:\\upload\\";
		  // out.println(loadpath);
		    // Create a factory for disk-based file items
		    FileItemFactory factory = new MonitoredDiskFileItemFactory(listener);
		   
		    // Create a new file upload handler
		    ServletFileUpload upload=new ServletFileUpload(factory);
		   
		    upload.setSizeMax(MAX_SIZE);
		  
		    List fileItems=null;
		    try
		    {    fileItems=upload.parseRequest(request);
		    
		    	 Iterator iter = fileItems.iterator(); 

		         while (iter.hasNext()) { 
		           FileItem item = (FileItem) iter.next();
		          
		           if (!item.isFormField()) { 
		            String name = item.getName();//获取文件全名  
		            name=name.substring(name.lastIndexOf("\\")+1);//文件名 
		            // out.println(name); 
		             long size = item.getSize(); 
		             if((name==null||name.equals("")) && size==0) 
		                continue; 
		           //  out.println(item.getName()+" Size="+item.getSize()+"<br>");//

		            File fNew= new File(loadpath, name);  
		            //  File fNew=new File("C:\\temp\\",name);
		            try {
		            	List<User> users=ExcelHandler.readInputStream(item.getInputStream());
		            	UserService us=new UserServiceImpl();
		            	us.importUsers(users);		            	
						//item.write(fNew);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}  
		           

		           } 
		         }
		    }
		       
		    
		    catch (FileUploadException e)
		    { if (e instanceof SizeLimitExceededException)
		       {
		    	
		       }
		        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
		    }
		    //response.getWriter().write("<script type=\"text/javascript\" src=\"../js/easyui/js/jquery-1.4.4.min.js\"></script>");
		    //response.getWriter().write("<script type=\"text/javascript\">$(function(){parent.open1('user/userList.jsp');});</script>");
			response.setCharacterEncoding(Constants.ENCODING_UTF8);
		    response.getWriter().write("<script type=\"text/javascript\">setInterval('goback()',2000); function goback(){parent.open1('user/userList.jsp');}</script>");
		    response.getWriter().write("数据已经导入,2秒内返回");
	
	}

}
